package com.spring.aop.cglib;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements org.springframework.cglib.proxy.MethodInterceptor {
	
	 private CustomDao customDao;
	public CglibProxy(CustomDao customDao) {
		this.customDao=customDao;
	}

	
	public CustomDao createProxy() {
		//1.创建cglib的核心类对象，（工具类）
		 Enhancer enhancer = new Enhancer();
		 
		 //2.设置父类
		 enhancer.setSuperclass(customDao.getClass());
		 
		 //3.设置回调函数，类似于InvocationHandler对象
		 enhancer.setCallback(this);
		 
		 //4.创建子类，即代理对象
		CustomDao proxy = (CustomDao) enhancer.create();
		 
		
		return proxy;
		
	}


	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		
		//判断，代码增强
		if ("find".equals(method.getName())) {
			System.out.println("代码增强：==========校验权限");
			return methodProxy.invokeSuper(proxy, args);
		}
		return methodProxy.invokeSuper(proxy, args);
	}


	


}
